package com.nexgo.payment.action.queryresp;

import android.support.annotation.NonNull;

import com.nexgo.payment.aop.annotation.RunOnMainThread;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.entity.TransData;
import com.nexgo.sound.SoundManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 余额查询输入操作
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年9月1日
 */
public class QueryRespPresenter implements QueryRespContract.Presenter {
    private Logger mLog = LoggerFactory.getLogger(QueryRespPresenter.class.getSimpleName());

    private final SoundManager mSoundManager;
    private QueryRespContract.View mView;
    private DataSource mRepository;
    private TransData mTransData;
    private boolean isFirstTimes = true;

    public QueryRespPresenter(@NonNull QueryRespContract.View view, @NonNull DataSource repository, @NonNull TransData transData) {
        mView = checkNotNull(view);
        mRepository = checkNotNull(repository);
        mTransData = checkNotNull(transData);
        view.setPresenter(this);
        mSoundManager = new SoundManager();
    }

    @Override
    public void start() {
        if (isFirstTimes) {
            mSoundManager.play(SoundManager.Type.TRANS_SUCC);
            postMainThread(() -> mView.showQueryResp(mTransData));
            isFirstTimes = false;
        }
    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {

    }

    @RunOnMainThread
    private void postMainThread(Runnable runnable) {
        if (mView.isActive()) {
            runnable.run();
        }
    }
}
